{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1dd4c8f4-0896-4491-9c1a-eac37432a419",
   "metadata": {},
   "source": [
    "# 数据整理，join, combine, reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "61abf415-3826-4b42-aaad-c065bc8a7203",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84b2e913-9d3b-483c-9974-c755a8d4d018",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 分层索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "71a80768-c71e-4d8b-aa41-06c75fa92845",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a  1    0.474658\n",
       "   2    0.900263\n",
       "   3    0.120982\n",
       "b  1    0.820022\n",
       "   3    0.634003\n",
       "c  1    0.820021\n",
       "   2    0.379741\n",
       "d  2    0.754178\n",
       "   3    0.269146\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.Series(np.random.uniform(size=9),\n",
    "                 index=[[\"a\", \"a\", \"a\", \"b\", \"b\", \"c\", \"c\", \"d\", \"d\"],\n",
    "                        [1, 2, 3, 1, 3, 1, 2, 2, 3]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5e962356-a21c-486e-be2d-6fe6c186e7cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([('a', 1),\n",
       "            ('a', 2),\n",
       "            ('a', 3),\n",
       "            ('b', 1),\n",
       "            ('b', 3),\n",
       "            ('c', 1),\n",
       "            ('c', 2),\n",
       "            ('d', 2),\n",
       "            ('d', 3)],\n",
       "           )"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c169ecd-3a1a-4989-b8ef-3ac8ff1a3061",
   "metadata": {},
   "source": [
    "### 部分索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ade571bc-549f-4bc4-8988-f6b36e32e310",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    0.820022\n",
       "3    0.634003\n",
       "dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"b\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "150f644a-3389-4869-b71d-4c59073f9a73",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b  1    0.820022\n",
       "   3    0.634003\n",
       "c  1    0.820021\n",
       "   2    0.379741\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"b\":\"c\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c1cff954-8b3f-4654-ad6c-fd3518d105dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b  1    0.820022\n",
       "   3    0.634003\n",
       "d  2    0.754178\n",
       "   3    0.269146\n",
       "dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[[\"b\",\"d\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7e9a3417-5486-4b45-857e-75b3984f7983",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0.900263\n",
       "c    0.379741\n",
       "d    0.754178\n",
       "dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二等级索引\n",
    "data.loc[:, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f22cf4cf-4186-43ba-99c9-1b169c08a1b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
